<?php

if (!defined('__BACKEND__'))
    die("you cannot directly access this page");
/*
 * by SONY K Wibisono
 * e-mail : sony@sonywibisono.com
 */
include ("class/news.php");
//Place code here
global $action;
function add_category(){
    echo "<form action=\"?mod=panel&file=news\" method=\"post\">
<input type=\"hidden\" name=\"action\" value=\"add_category_process\" />         
<table>
<tr><td>Id</td><td> </td></tr>
<tr><td>Category Name</td><td><input type=\"text\" name=\"category_name\" class=\"textbox\"/> </td></tr>
<tr><td>Parent</td><td>";
    select_post_category('');
    echo "</td></tr>
<tr><td></td><td><input type=\"submit\" value=\"Save\"/>
<input type=\"button\" name=\"cancel\" value=\"Cancel\" onclick=\"javascript:history.go(-1);\"/></tr></table></form>";
}
function edit_category(){
    global $db,$id;
    $sql="select * from _post_category where id=$id";
    $row=$db->fetch_row($sql);
    extract($row);
    echo "<form action=\"?mod=panel&file=news\" method=\"post\">
<input type=\"hidden\" name=\"cat_id\" value=\"$cat_id\" />        
<input type=\"hidden\" name=\"action\" value=\"edit_category_process\" /> 
<table>
<tr><td>Id</td><td>$id </td></tr>
<tr><td>Category Name</td>
    <td><input type=\"text\" name=\"category_name\" class=\"textbox\" value=\"$category_name\"/> </td></tr>
<tr><td>Parent</td><td>";
    select_post_category($parent);
    echo "</td></tr>
<tr><td></td><td><input type=\"submit\" value=\"Save\"/>
<input type=\"button\" name=\"cancel\" value=\"Cancel\" onclick=\"javascript:history.go(-1);\"/></td></tr></table></form>";
}
function news_category_count($category){
    global $db;
    $sql="select count(*) as jumlah from _post_contents where post_category=$category";
    $result=$db->fetch_row($sql);
    if (!empty($result)){
        return $result[0];
    } else return 0;
}
function list_category(){
    global $page,$db;
    $sql="select * from _post_category order  by category_name";
    $result=$db->query($sql);
    echo "<h2>News Catgory</h2>
        <table width=\"100%\" cellspacing='6'><tr><th>No</th><th>Category Name</th><th>News</th><th>Action</th></tr>";
    $i=0;
    while ($row=$db->fetch_array($result)){
        extract($row);
        $i++;
        $newscount=news_category_count($id);
        echo "<tr><td align=right>$i</td><td>$category_name</td><td align=\"right\">$newscount</td>
        <td align=\"center\">
        <a href=\"?mod=panel&file=news&action=add_category\">
<img class='icon' src=\"images/add.png\" title=\"Add\"/></a><a href=\"?mod=panel&file=news&action=edit_category&id=$id\">
            <img class=\"icon\" src=\"images/edit.png\" title=\"Edit\"/></a>
        <a href=\"?mod=panel&file=news&action=delete_category&id=$id\">
            <img class=\"icon\" src=\"images/delete.png\" title=\"Delete\" /></a> 
            </td></tr>";
    }
    echo "</table>";
}
function add_form(){
    echo "<form action=\"?mod=panel&file=news\" method=\"post\">
        <input type=\"hidden\" name=\"action\" value=\"add_process\" />  
<table>
<tr><td><h2>Add News</h2></td><td> </td></tr>
<tr><td>Title</td><td><input type=\"text\" name=\"title\" class=\"textbox\" size=\"50\"/> </td></tr>
<tr><td>Post Category</td><td>";
    select_post_category('');
    echo "</td></tr>
<tr><td valign=\"top\">Excerpt</td>
<td><textarea name=\"excerpt\" class=\"textbox\" cols=\"60\" rows=\"10\"></textarea> </td></tr>        
<tr><td valign=\"top\">Contents</td>
<td><textarea name=\"contents\" rel=\"editor\" cols=\"60\" rows=\"10\"></textarea> </td></tr>
<tr><td valign=\"top\">Tag</td>
<td><input type=\"text\" name=\"tags\" class=\"textbox\" size=\"60\"/> </td></tr>
<tr><td></td><td><input type=\"submit\" value=\"Add News\"/>
<input type=\"button\" name=\"cancel\" value=\"Cancel\" onclick=\"javascript:history.go(-1);\"/></td></tr></table>        
</form>";
    
}
function add_process(){
    global $db,$title,$post_category,$excerpt,$contents,$user_id,$date_create,$hits,$tags;
    if ($post_category=='0')$post_category=1;
    $search=array('\\',"'");
    $replace=array('',"\'");
    $contents=str_replace("'", " ", $contents);
    $news=new NewsContents();
    $news->post_category=$post_category;
    $news->excerpt=$excerpt;
    $news->contents=$contents;
    $news->title=$title;
    $news->user_creator=$user_id;
    $news->hits=0;
    $post_id=$news->insert();
    insert_all_tags($post_id,$tags);
    refresh_to("?mod=panel&file=news", "Back to News List", 2);
}
function insert_all_tags($id,$tags){
    global $db;
    if (!empty($tags)){
    $wordsid=insert_tagged($tags);
    $word=explode(',',$wordsid);
    for ($i=0;$i<count($word);$i++){
        if(!is_tag_exist($word[$i],$id)&&$word[$i]!='0'){
        $sql_insert="insert into _post_tags values ($id,{$word[$i]})";
        $db->query($sql_insert);
        }
    }
    }
}
function edit_form(){
    global $db,$id,$cat;
    if (!empty($id)){
        $sql="select * from _post_contents where id=$id";
        $row=$db->fetch_row($sql);
        if (!empty($row)){
            extract($row);
        }
    echo "<form action=\"?mod=panel&file=news\" method=\"post\">
        <input type=\"hidden\" name=\"action\" value=\"edit_process\" />  
        <input type=\"hidden\" name=\"id\" value=\"$id\" />
<table>
 
<tr><td>Title</td></tr>
        <tr><td><input type=\"text\" name=\"title\" class=\"textbox\" value=\"$title\" size=\"50\"/></td></tr>
<tr><td>Post Category</td></tr><tr><td>";
    select_post_category($post_category);
    echo "</td></tr>
<tr><td valign=\"top\">Excerpt</td></tr><tr>
<td><textarea name=\"excerpt\" class=\"textbox\" cols=\"60\" rows=\"4\">$excerpt</textarea> </td></tr>     
<tr><td>Contents</td></tr><tr><td><textarea name=\"contents\" class=\"textbox\" rel=\"editor\">$contents</textarea> </td></tr>
<tr><td valign=\"top\">Tag</td></tr><tr>
<td><input type=\"text\" name=\"tags\" class=\"textbox\" size=\"60\"/> </td></tr>
<tr><td><input type=\"submit\" value=\"Save\"/>
<input type=\"button\" name=\"cancel\" value=\"Cancel\" onclick=\"javascript:history.go(-1);\"/></td></tr></table>        
</form>";
    }
}
function edit_process(){
    global $db,$id,$title,$post_category,$excerpt,$contents,$user_id,$date_create,$hits,$tags;
    //echo "$tags";
    if ($post_category=='0')$post_category=1;
    $search=array('/',"'");
    $replace=array('',"\'");
    $contents=str_replace("'", " ", $contents);
    $words=insert_tagged($tags);
    //echo $words;
    $news=new NewsContents();
    $news->id=$id;
    $news->post_category=$post_category;
    $news->excerpt=$excerpt;
    $news->contents=$contents;
    $news->title=$title;
    $news->user_creator=$user_id;
    $news->hits=0;
    $news->update();
    
    insert_all_tags($id,$tags);
     
    refresh_to("?mod=panel&file=news", "Back to News List", 2);
}
function delete(){
    global $db,$id;
    $sql="delete from _post_contents where id=$id";
    $db->query($sql);
    refresh_to("?mod=panel&file=news", "Back to News List", 2);
}
function list_table(){
    global $db,$page,$pagesize;
    if (empty($page)){
        $start=0;
    } else $start=$page*$pagesize;
    $sql="select a.*,b.category_name from _post_contents a,_post_category b 
        where a.post_category=b.id order by a.id asc limit $start,$pagesize";
        $result=$db->query($sql);
    echo "<a href=\"?mod=panel\"><img class=\"icon\" src=\"images/panel.png\" title=\"Back to Panel\"/></a>
        <a href=\"?mod=panel&file=news&action=add\">
        <img class=\"icon\" src=\"images/panel/add_news.png\" title=\"Add News\"/></a>";
    echo "<div>";
    //;
    //for($i=0;$i<count($row);$i++){
    while ($row=$db->fetch_array($result)){  
    extract($row);
        echo "<div class=\"row_news\"><div class=\"title\">Title : <b>$title</b></div>
    <div id=\"excerpt_$id\" class=\"excerpt\">$excerpt</div>
    <b>Category : </b> $category_name, Hits : $hits 
    <div id=\"menu_$id\">
    <a id=\"news_$id\" href=\"#\">
    <img class=icon src=\"images/browse.png\" title=\"Edit\" /></a>
    <a href=\"?mod=panel&file=news&action=edit&id=$id\">
    <img class=icon src=\"images/edit.png\" title=\"Edit\" /></a>
    <a href=\"?mod=panel&file=news&action=delete&id=$id\">
    <img class=icon src=\"images/delete.png\" title=\"Hapus\" /></a></div>
    <script>
    $('#excerpt_$id').hide();
    $('#news_$id').click(function (){
    $('#excerpt_$id').toggle('slow');
    });
    </script>
    </div>";
    }
    echo "</div>";
    
}
switch($action){
    case 'add_category':
        add_category();
        break;
    case 'add_process':
        add_process();
        break;
    case 'add':
        add_form();
        break;
    case 'edit_category':
        edit_category();
        break;
    case 'edit':
        edit_form();
        break;
    case 'edit_process':
        edit_process();
        break;
    case 'delete':
        delete();
        break;
    default:
        list_table();
        break;
}
?>